﻿//-----------------------------------------------------------------------------------------------------------
// WBFSSync Project by Omega Frost 
// http://wbfssync.codeplex.com/
//
// WBFSSync is Licensed under the terms of the 
// Microsoft Reciprocal License (Ms-RL)
//
// special thanks to Chistian Mosers
// from http://www.wpftutorial.net
// for some layout and windows 7 routines
//-----------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Interop;
using System.Runtime.InteropServices;
using System.Windows.Media;

namespace WbfsSync.Themes
{
    //-------------------------------------------------------------------------------------------------------
    //
    //-------------------------------------------------------------------------------------------------------
    public static class NativeLayout
    {
        //---------------------------------------------------------------------------------------------------
        // Structs
        //---------------------------------------------------------------------------------------------------

        [StructLayout(LayoutKind.Sequential)]
        struct MARGINS
        {
            public int cxLeftWidth;
            public int cxRightWidth;
            public int cyTopHeight;
            public int cyBottomHeight;
        }

        //---------------------------------------------------------------------------------------------------
        // Constants
        //---------------------------------------------------------------------------------------------------

        const int GWL_EXSTYLE = -20;
        const int WS_EX_DLGMODALFRAME = 0x0001;
        const int SWP_NOSIZE = 0x0001;
        const int SWP_NOMOVE = 0x0002;
        const int SWP_NOZORDER = 0x0004;
        const int SWP_FRAMECHANGED = 0x0020;
        const uint WM_SETICON = 0x0080;

        //---------------------------------------------------------------------------------------------------
        // Native routines
        //---------------------------------------------------------------------------------------------------

        [DllImport("user32.dll")]
        static extern int GetWindowLong(IntPtr hwnd, int index);

        [DllImport("user32.dll")]
        static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);

        [DllImport("user32.dll")]
        static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
                   int x, int y, int width, int height, uint flags);

        [DllImport("user32.dll")]
        static extern IntPtr SendMessage(IntPtr hwnd, uint msg,
                   IntPtr wParam, IntPtr lParam);

        [DllImport("dwmapi.dll")]
        static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset);

        [DllImport("dwmapi.dll")]
        extern static int DwmIsCompositionEnabled(ref int en);

        //---------------------------------------------------------------------------------------------------
        // Routines
        //---------------------------------------------------------------------------------------------------

        //---------------------------------------------------------------------------------------------------
        // Removes the icon from a WPF window
        //---------------------------------------------------------------------------------------------------
        public static void RemoveIcon(Window window)
        {
            try
            {
                IntPtr hwnd = new WindowInteropHelper(window).Handle;

                if (Environment.OSVersion.Version.Major >= 6) //Vista / Server 2008 / Seven
                {
                    int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
                    SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_DLGMODALFRAME);

                    SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE |
                          SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
                }
                else //XP
                {
                    SendMessage(hwnd, WM_SETICON, IntPtr.Zero, IntPtr.Zero);
                }
            }
            catch
            {

            }
        }

        //---------------------------------------------------------------------------------------------------
        // Extends the Glass frame
        //---------------------------------------------------------------------------------------------------
        public static void ExtendGlass(Window window, Thickness margin)
        {
            try
            {
                int isGlassEnabled = 0;
                DwmIsCompositionEnabled(ref isGlassEnabled);
                if (Environment.OSVersion.Version.Major > 5 && isGlassEnabled > 0)
                {
                    // Get the window handle
                    WindowInteropHelper helper = new WindowInteropHelper(window);
                    HwndSource mainWindowSrc = (HwndSource)HwndSource.
                        FromHwnd(helper.Handle);
                    mainWindowSrc.CompositionTarget.BackgroundColor =
                        Colors.Transparent;

                    // Get the dpi of the screen
                    System.Drawing.Graphics desktop =
                       System.Drawing.Graphics.FromHwnd(mainWindowSrc.Handle);

                    float dpiX = desktop.DpiX / 96;
                    float dpiY = desktop.DpiY / 96;

                    // Set Margins
                    MARGINS margins = new MARGINS();
                    margins.cxLeftWidth = (int)(margin.Left * dpiX);
                    margins.cxRightWidth = (int)(margin.Right * dpiX);
                    margins.cyBottomHeight = (int)(margin.Bottom * dpiY);
                    margins.cyTopHeight = (int)(margin.Top * dpiY);

                    window.Background = Brushes.Transparent;

                    int hr = DwmExtendFrameIntoClientArea(mainWindowSrc.Handle,
                                ref margins);
                }
                else
                {
                    window.Background = SystemColors.WindowBrush;
                }
            }
            catch (DllNotFoundException)
            {

            }
        }
    }
}
